/* Copyright (C) 2005-2013, Unigine Corp. All rights reserved.
 *
 * File:    deferred_base.h
 * Desc:    Base deferred shader
 * Version: 1.01
 * Author:  Alexander Zapryagaev <frustum@unigine.com>
 *
 * This file is part of the Unigine engine (http://unigine.com/).
 *
 * Your use and or redistribution of this software in source and / or
 * binary form, with or without modification, is subject to: (i) your
 * ongoing acceptance of and compliance with the terms and conditions of
 * the Unigine License Agreement; and (ii) your inclusion of this notice
 * in any version of this software that you use or redistribute.
 * A copy of the Unigine License Agreement is available by contacting
 * Unigine Corp. at http://unigine.com/
 */

#ifndef __DEFERRED_BASE_H__
#define __DEFERRED_BASE_H__

/******************************************************************************\
*
* Deferred depth
*
\******************************************************************************/

/*
 */
fixed4 setDeferredDepth(float distance,half glow) {
	half2 deferred;
	distance = sqrt(distance) * 65536.0f;
	deferred.x = floor(distance * (1.0f / 256.0f));
	distance -= deferred.x * 256.0f;
	deferred.y = floor(distance);
	deferred = float2(255.0f,255.0f) - deferred;
	return float4(deferred * (1.0f / 255.0f),glow * (1.0f / 16.0f),0.0f);
}

/*
 */
half getDeferredDepth(fixed4 deferred) {
	half2 factor = float2(65280.0f / 65536.0f,255.0f / 65536.0f);
	half distance = 1.0f - dot(deferred.xy,factor);
	return distance * distance;
}

fixed getDeferredGlow(fixed4 deferred) {
	return deferred.z * 16.0f;
}

#endif /* __DEFERRED_BASE_H__ */
